package com.group.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.group.dao.TbRoleMapper;
import com.group.pojo.Common;
import com.group.pojo.TbRole;
import com.group.pojo.TbRoleExample;
import com.group.pojo.TbRoleExample.Criteria;
import com.group.pojo.TbUser;
import com.group.pojo.TbUserExample;
import com.group.service.CommonService;
import com.group.service.RoleService;
import com.group.util.CommonUtil;

@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private TbRoleMapper roleMapper;
	@Autowired
	private CommonService commonService;

	public List<TbRole> getAllRoles() {
		TbRoleExample example = new TbRoleExample();
		example.createCriteria().andRoleStatusEqualTo("1");
		List<TbRole> list = roleMapper.selectByExample(example);
		return list;
	}

	public Map<String, String> save(HttpServletRequest request, TbRole role) {
		TbUser user = (TbUser) request.getSession().getAttribute("USER_SESSION");
		//设置创建人、创建时间
		role.setCreateUserId(user.getId());
		role.setCreateTime(new Date());
		
		//设置主键id
		String currentDate = CommonUtil.getCurrentDate("yyyyMMdd");
		String nextId = commonService.getTbCurrentDateNextId(new Common("tb_role", currentDate));
		role.setId(nextId);
		
		//设置状态(虽然数据表建立的时候可能默认值是1，但是为了程序的健壮性，还是要设置一下)
		role.setRoleStatus("1");
		
		int result = roleMapper.insertSelective(role);
		
		Map<String, String> map = CommonUtil.getReturnMap("success", "false");
		if (result == 1) {
			map.put("success", "true");
		}
		return map;
	}


	public Map<String, String> batchDelete(String[] roleIds) {
		TbRole recode = new TbRole();
		recode.setRoleStatus("0");
		
		Map<String, String> map = CommonUtil.getReturnMap("success", "true");
		for (String roleId : roleIds) {
			recode.setId(roleId);
			int update = roleMapper.updateByPrimaryKeySelective(recode);
			if (update != 1) {
				map.put("success", "false");
			}
		}
		return map;
	}

	public TbRole getRoleById(String id) {
		TbRole role = roleMapper.selectByPrimaryKey(id);
		return role;
	}

	public Map<String, String> update(TbRole role) {
		int update = roleMapper.updateByPrimaryKeySelective(role);
		
		Map<String, String> map = CommonUtil.getReturnMap("success", "false");
		if (update == 1) {
			map.put("success", "true");
		}
		return map;
	}

	@Override
	public String roleCheckRepect(TbRole role) {
		TbRoleExample example = new TbRoleExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleStatusEqualTo("1");
		
		String roleName = role.getRoleName();
		if (roleName != null && !"".equals(roleName)) {
			criteria.andRoleNameEqualTo(roleName);
		}
		
		int counts = roleMapper.countByExample(example);
		if (counts == 0) {
			return "true";
		}
		return "false";
	}
}
